import "antd/lib/upload/style/css";
import _Upload from "antd/lib/upload";
import "antd/lib/button/style/css";
import _Button from "antd/lib/button";
import "antd/lib/message/style/css";
import _message from "antd/lib/message";

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

import React from 'react';
import PropTypes from 'prop-types';
import { onBeforeUpload, createParamsUrl } from './util';
/**
 * 文件导入组件  （本质依然是上传文件）
 */

function FileImport(props) {
  var api = props.api,
      onFinish = props.onFinish,
      text = props.text,
      res = _objectWithoutProperties(props, ["api", "onFinish", "text"]);

  return /*#__PURE__*/React.createElement(_Upload, _extends({}, res, {
    // todo Client Authorization 要么在url里面，要么在heasers里面，具体看后端要求
    action: createParamsUrl({}, undefined, api) // headers={{
    //    Client: config.client,
    //   Authorization: localStorage.getItem('token'),
    // }}
    ,
    showUploadList: false,
    beforeUpload: onBeforeUpload,
    onChange: function onChange(_ref) {
      var file = _ref.file,
          fileList = _ref.fileList;

      if (file && file.response) {
        if (file.response.success) {
          _message.success('上传成功');

          if (onFinish) {
            onFinish(file.response);
          }
        } else {
          _message.error(file.response.message);
        } // 失败文件下载处理
        // todo 失败文件一般两种处理方式，1前端主动下载，2后端直接返回，本次是1
        // todo 该处需要配合新项目改动，具体看后端参数字段名称


        if (!file.response.success && file.response.models) {
          if (Array.isArray(file.response.models)) {
            file.response.models.forEach(function (i) {
              var url = createParamsUrl({
                fileId: i.id,
                fileCode: i.code
              }, 'download');
              var createA = document.createElement('a');
              createA.download = i.originalName;
              createA.href = url;
              document.body.append(createA); // 不添加 firefix 无法触发点击

              createA.click();
              createA.remove();
            });
          }
        }
      }
    }
  }), /*#__PURE__*/React.createElement(_Button, {
    type: "primary",
    style: {
      marginRight: 8
    }
  }, text || '导入'));
}
/*
 * 核心参数
 * */


FileImport.propTypes = {
  api: PropTypes.string,
  onFinish: PropTypes.func,
  text: PropTypes.string // 默认为 导入

};
export default FileImport;